import 'package:file_transfer/pojo/message.dart';

abstract class ChannelMessage{

  static const int systemType = -1;
  static const int fileType = 1;
  static const int textType = 0;

  int type;
  User? user;
  bool local;
  ChannelMessage(this.type,{this.user, required this.local});

}


// 系统消息
enum SystemMessageType{
  success,
  fail
}
class SystemMessage extends ChannelMessage{
  final String name;
  final SystemMessageType systemMessageType;

  SystemMessage(this.name,{this.systemMessageType = SystemMessageType.fail}):super(ChannelMessage.systemType,local: true);
}

class FileChannelMessage extends ChannelMessage{

  int size;
  String name;

  FileChannelMessage(this.name,this.size,{required super.local,super.user}):super(ChannelMessage.fileType);

  factory FileChannelMessage.fromJsonLocal(Map<String,dynamic> data){
    return FileChannelMessage(data["name"], data["size"], local: true);
  }
  factory FileChannelMessage.fromJsonRemote(Map<String,dynamic> data){
    return FileChannelMessage(data["name"], data["size"], local: false,user:User(name: data["client"]));
  }

  @override
  String toString() {
    return 'FileChannelMessage{size: $size, name: $name}';
  }
}

class TextChannelMessage extends ChannelMessage{

  String text;

  TextChannelMessage(this.text,{required super.local ,super.user}):super(ChannelMessage.fileType);

  factory TextChannelMessage.fromJsonLocal(Map<String,dynamic> data){
    return TextChannelMessage(data["data"], local: true);
  }
  factory TextChannelMessage.fromJsonRemote(Map<String,dynamic> data){
    return TextChannelMessage(data["data"], local: false ,user:User(name: data["name"]));
  }

  @override
  String toString() {
    return 'TextChannelMessage{text: $text}';
  }
}

